package com.example.tests;


import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.logging.Logger;


import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import com.example.fw.GroupObject;
import com.example.fw.Groups;


public class GroupCreationTests extends TestBase{
	
	private Logger log = Logger.getLogger("GroupCreationTests");
	//private int counter; 
		
	@Test(dataProvider = "randomGroups", dataProviderClass = GroupDataGenerator.class)

	// oldList and newList are taken from DB	
	public void testValidGroupCanBeCreated(GroupObject validGroup) throws Exception {
		log.info("GroupCreationTests start");
		Groups oldList = app.getHibernateHelper().getGroups();
		log.info("oldList=" + oldList);
		log.info("validGroup=" + validGroup);
		app.getGroupHelper().createGroup(validGroup);
		Groups newList = app.getHibernateHelper().getGroups();
		log.info("newList=" + newList);
		assertThat(newList, equalTo(oldList.withAdded(validGroup)));
		log.info("GroupCreationTests end");
	}


//	@Test
	public void testGroupWithEmptyDataCanBeCreated() throws Exception {
		GroupObject emptyDataGroup = new GroupObject()
		.setName("").setHeader("").setFooter("");
		Groups oldList = app.getHibernateHelper().getGroups();
		app.getGroupHelper().createGroup(emptyDataGroup);
		Groups newList = app.getHibernateHelper().getGroups();
		assertThat(newList, equalTo(oldList.withAdded(emptyDataGroup)));
	}
	
	@AfterMethod
	public void testUIandDBConformsWithModel(){
		/*if (counter > 10){
		Groups modelList = app.getGroupHelper().getGroups();
		Groups dbList = app.getHibernateHelper().getGroups();
		Groups uiList = app.getGroupHelper().getUiGroups();
		assertThat(dbList, equalTo(modelList));
		assertThat(uiList, equalTo(modelList));
		counter = 0;
		}else {
			counter = counter + 1;
		}*/
		if (app.getProperty("ui.check") != null){
			Groups modelList = app.getGroupHelper().getGroups();
			Groups uiList = app.getGroupHelper().getUiGroups();
			assertThat(uiList, equalTo(modelList));
		}
		if (app.getProperty("db.check") != null){
			Groups modelList = app.getGroupHelper().getGroups();
			Groups dbList = app.getHibernateHelper().getGroups();
			assertThat(dbList, equalTo(modelList));
		}
	}
	
}
